#
# @lc app=leetcode.cn id=242 lang=python3
#
# [242] 有效的字母异位词
#


# @lc code=start
class Solution:
    def isAnagram_1(self, s: str, t: str) -> bool:
        tmp = {}
        for i in s:
            if i in tmp:
                tmp[i] += 1
            else:
                tmp[i] = 1
        for i in t:
            if i not in tmp or tmp[i] == 0:
                return False
            else:
                tmp[i] -= 1
        for i in tmp.values():
            if i > 0:
                return False
        return True

    def isAnagram_2(self, s: str, t: str) -> bool:
        s = sorted(s)
        t = sorted(t)
        return s == t

    def isAnagram(self, s: str, t: str) -> bool:
        s_set = set(s)
        t_set = set(t)
        
        if len(s_set) != len(t_set):
            return False
        
        for i in s_set:
            if s.count(i) != t.count(i):
                return False
        return True


# @lc code=end

if __name__ == "__main__":
    S = Solution()

    s = "anagram"
    t = "nagaram"
    print(S.isAnagram(s, t))

    s = "rat"
    t = "car"
    print(S.isAnagram(s, t))

    s = "a"
    t = "ab"
    print(S.isAnagram(s, t))
